import Vue from 'vue'
import * as Util from '../components/Util'

let FileService = {
  install (Vue, options) {
    // 给vue增添对话框显示方法
    Vue.FileService = Vue.prototype.$FileService = FileService
  },

  fileSave (data) {
    let result = Object.assign({}, result, data.baseinfo.base)
    // result.f_userinfo_id = result.f_userinfo_id ? result.f_userinfo_id : null
    result.f_user_type = result.f_user_type.length > 0 ? data.baseinfo.base.f_user_type[0] : ''
    result.f_cost_type = result.f_cost_type.length > 0 ? data.baseinfo.base.f_cost_type[0] : ''
    result.f_gasproperties = result.f_gasproperties.length > 0 ? data.baseinfo.base.f_gasproperties[0] : ''
    result.f_credentials = result.f_credentials.length > 0 ? data.baseinfo.base.f_credentials[0] : ''
    if (data.baseinfo.base.f_cost_type[0] === '银行代扣') {
      result.infobank = data.baseinfo.bank
    }
    result.f_operator = Util.f.name
    result.f_filiale = Util.f.f_fengongsi
    result.f_outlets = Util.f.f_parentname
    result.f_user_state = result.f_user_state ? result.f_user_state : '正常'
    result.userfiles = []
    data.meterinfo.forEach((item) => {
      // item.f_user_id = result.f_user_id ? result.f_user_id : null
      let meter = Object.assign({}, meter, item)
      meter.f_position = item.f_position.length > 0 ? item.f_position[0] : ''
      meter.f_gasbrand_id = item.gasbrand[0].id
      meter.f_inputtor = item.f_inputtor.length > 0 ? item.f_inputtor[0] : ''
      meter.f_install_person = item.f_install_person.length > 0 ? item.f_install_person[0] : ''
      meter.f_gasmodel_id = item.gasmodel[0].id
      meter.f_user_type = result.f_user_type.length > 0 ? data.baseinfo.base.f_user_type[0] : ''
      meter.f_gasproperties = result.f_gasproperties.length > 0 ? data.baseinfo.base.f_gasproperties[0] : ''

      if (!item.f_user_id) {
        item.gasbrand[0].f_meter_type === '物联网表' ? meter.f_table_state = '未发送' : meter.f_table_state = '正常'
        item.gasbrand[0].f_meter_type.includes('卡表') ? meter.f_whether_hairpin = '未发' : meter.f_whether_hairpin = '无卡'
        meter.f_aroundmeter = '左表'
      }
      meter.f_initial_base = item.f_meter_base
      meter.f_meter_classify = item.gasbrand[0].f_meter_type
      meter.f_price_id = item.pricename[0].f_price_id
      meter.f_filiale = Util.f.f_fengongsi
      meter.f_outlets = Util.f.f_parentname
      result.userfiles.push(meter)
    })
    let devices = []
    data.devicesinfo.forEach((item) => {
      let device = Object.assign({}, device, item)
      device.f_devices_type = item.f_devices_type[0]
      device.f_devices_state = item.f_devices_state ? item.f_devices_state : '有效'
      devices.push(device)
    })
    return Vue.resetpost('rs/logic/fileSave', {data: {userinfo: result, devices: devices}})
  },
  batchOperate (model, type) {
    let data = {
      f_operat_type: '销户',
      f_describe: `对用户${model.userinfo.f_user_name}进行销户`,
      f_state: '有效',
      f_user_id: model.userinfo.f_user_id,
      record_userinfo: model.userinfo.f_userinfo_id,
      f_user_name: model.userinfo.f_user_name,
      f_cancel_state: type,
      f_cancel_cause: model.f_reason,
      f_operator: Util.f.name,
      f_filiale: Util.f.f_fengongsi,
      f_outlets: Util.f.f_parentname
    }
    if (type === '销户') {
      data.userinfo = {
        f_userinfo_id: model.userinfo.f_userinfo_id,
        f_user_state: '销户',
        version: model.userinfo.version
      }
    } else if (type === '启用') {
      data.userinfo = {
        f_userinfo_id: model.userinfo.f_userinfo_id,
        f_user_state: '正常',
        version: model.userinfo.version
      }
    }
    return Vue.resetpost('rs/entity/t_cancellation', data, {resolveMsg: `${type}成功`, rejectMsg: `${type}失败`})
  }
}

export default FileService
